package cn.taichoo.server.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import cn.taichoo.server.mapper.AppMapper;
import cn.taichoo.server.domain.AppEntity;
import cn.taichoo.server.service.IAppService;

import javax.annotation.Resource;

/**
 * App主，基本信息Service业务层处理
 *
 * @author Wuzh
 * @date 2025-03-17
 */
@Service
public class AppServiceImpl extends ServiceImpl<AppMapper, AppEntity> implements IAppService {

    @Resource
    private AppMapper appMapper;

    /**
     * 查询App主，基本信息
     *
     * @param id App主，基本信息主键
     * @return App主，基本信息
     */
    @Override
    public AppEntity selectAppEntityById(Long id) {
        return appMapper.selectAppEntityById(id);
    }

    /**
     * 查询App主，基本信息列表
     *
     * @param appEntity App主，基本信息
     * @return App主，基本信息
     */
    @Override
    public List<AppEntity> selectAppEntityList(AppEntity appEntity) {
        return appMapper.selectAppEntityList(appEntity);
    }

    @Override
    public AjaxResult insert(AppEntity appEntity) {
        appEntity.setCreateBy(SecurityUtils.getUserId());
        boolean save = this.save(appEntity);
        if (!save) {
            throw new RuntimeException("新增失败！");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateAppById(AppEntity appEntity) {
        appEntity.setUpdateBy(SecurityUtils.getUserId());
        boolean b = this.updateById(appEntity);
        if (!b) {
            throw new RuntimeException("修改失败！");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteById(Long id) {
        boolean update = this.update(new LambdaUpdateWrapper<AppEntity>().
                set(AppEntity::getDeleted, 1).
                eq(AppEntity::getId, id));
        if (!update) {
            throw new RuntimeException("删除失败！");
        }
        return AjaxResult.success();
    }

    @Override
    public AppEntity getAppByPack(String packageName) {
        AppEntity one = this.getOne(new LambdaQueryWrapper<AppEntity>().eq(AppEntity::getPackageName, packageName));
        return one;
    }

}
